#pragma once
#include "Objects\object.h"
#include "BaseTile.h"
#include "Line.h"
#include <vector>
#include <map>
struct star::Context;
class Grid :
	public star::Object
{
public:
	Grid(void);
	~Grid(void);

	void SwichtTileWithRightSide(const ivec2& leftBottomPos);
	void SwichtTileWithRightSide(uint8 left, uint8 bottom);
	uint8 GetNrOfHorizontalTiles() const;
	uint8 GetNrOfVerticalTiles() const;
	uint8 GetSafeZone() const;
	uint8 GetNrOfTilesInColumn(uint8 col);
	void AddLine();

	void Update(const star::Context& context);

	void GenerateLevel();
	void ReGenerateLevel();
	bool GameOver() const;
	void ResetGrid();

protected:
	void Initialize();
	void AfterInitialzed();

private:
	static const int8 MAX_TILES_HEIGHT = 14;
	static const int8 MAX_TILES_WIDTH = 15;
	static const int8 SAFE_ZONE_LIMIT = 13;
	static const int32 LEFT_BORDER = -5;
	static const int32 RIGHT_BORDER = 150;
	BaseTile* m_TileArr[MAX_TILES_WIDTH][MAX_TILES_HEIGHT];
	std::map<uint8,std::vector<ivec2>> m_TilesToDelete;
	std::map<uint8,int> m_FoundIDs;
	std::map<uint8,int> m_PreviousIDs;
	Line* m_pLineObj;

	int8 m_TileWidth;
	int32 m_FieldWidth;
	int8 m_CurrentTileID,m_Multiplier;
	int8 m_Colom;
	bool m_Left, m_Right, m_Top, m_Bottom;
	bool m_LeftEdge, m_RightEdge, m_TopEdge, m_BottomEdge;
	bool m_GameOver;
	bool m_FoundIDPrevColom,m_LineAdded;

	void SwichTiles(uint8 width1, uint8 height1, uint8 width2, uint8 height2);

	ivec2 CheckGravity(uint8 width, uint8 height);
	void CheckGlobalGravity();
	void CheckForSquare(const ivec2& coords);
	void CheckForSquare(uint8 width, uint8 height);
	void CheckForSquareGlobal();
	void CheckColumnForGameOver(uint8 col);

	bool CompareTileColors(uint8 width1, uint8 height1, uint8 width2, uint8 height2);
	void MakeSquare(uint8 left, uint8 bottom, uint8 right, uint8 top, int8 tileID);
	void SetTileIDs(uint8 left1, uint8 bottom1, uint8 left2, uint8 bottom2, int8 tileID);
	void CheckAdjacentTilesForID(int8 width, int8 height);
	int8 MakeNewTileID();
};

